<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="班组编号" prop="teamCode">
        <el-input
          v-model="queryParams.teamCode"
          placeholder="请输入班组编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班组名称" prop="teamName">
        <el-input
          v-model="queryParams.teamName"
          placeholder="请输入班组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          :icon="Plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:team:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          :icon="Edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:team:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :icon="Delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:team:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          :icon="Download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:team:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班组ID" align="center" prop="teamId" />
      <el-table-column label="班组编号" align="center" prop="teamCode" />
      <el-table-column label="班组名称" align="center" prop="teamName" />
      <el-table-column label="班组类型" align="center" prop="calendarType" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            :icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:team:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            :icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:team:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改班组对话框 -->
    <el-dialog v-model="open" :title="title" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="班组编号" prop="teamCode">
          <el-input v-model="form.teamCode" placeholder="请输入班组编号" maxlength="30" show-word-limit class="custom-input" />
        </el-form-item>
        <el-form-item label="班组名称" prop="teamName">
          <el-input v-model="form.teamName" placeholder="请输入班组名称" maxlength="50" show-word-limit class="custom-input" />
        </el-form-item>
        <el-form-item label="班组类型" prop="calendarType">
          <el-select v-model="form.calendarType" placeholder="请选择班组类型" class="custom-input" clearable>
            <el-option v-for="dict in calendarTypeOptions" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注信息" maxlength="200" show-word-limit rows="3" class="custom-input" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm(formRef)">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue'
import { listTeam, getTeam, delTeam, addTeam, updateTeam } from "@/api/system/team"
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Plus, Edit, Delete, Download } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance()
const formRef = ref(null)
const loading = ref(true)
const open = ref(false)
const title = ref('')
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const teamList = ref([])
const showSearch = ref(true)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teamCode: null,
    teamName: null,
    calendarType: null
  },
  rules: {
    teamCode: [
      { required: true, message: "班组编号不能为空", trigger: "blur" },
      { min: 3, max: 30, message: "长度在 3 到 30 个字符", trigger: "blur" }
    ],
    teamName: [
      { required: true, message: "班组名称不能为空", trigger: "blur" },
      { min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur" }
    ],
    calendarType: [
      { required: true, message: "班组类型不能为空", trigger: "change" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

const calendarTypeOptions = ref([
  { label: '标准班组', value: 'STANDARD' },
  { label: '轮班班组', value: 'SHIFT' },
  { label: '临时班组', value: 'TEMPORARY' }
])

/** 查询班组列表 */
function getList() {
  loading.value = true
  listTeam(queryParams.value).then(response => {
    teamList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    teamId: null,
    teamCode: null,
    teamName: null,
    calendarType: null,
    remark: null
  }
  proxy.resetForm("formRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm")
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加班组"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const teamId = row.teamId || ids.value
  getTeam(teamId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改班组"
  })
}

/** 提交按钮 */
function submitForm(formEl) {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      if (form.value.teamId != null) {
        updateTeam(form.value).then(response => {
          ElMessage.success("修改成功")
          open.value = false
          getList()
        })
      } else {
        addTeam(form.value).then(response => {
          ElMessage.success("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const teamIds = row?.teamId || ids.value
  ElMessageBox.confirm('是否确认删除班组编号为"' + teamIds + '"的数据项？').then(() => {
    return delTeam(teamIds)
  }).then(() => {
    getList()
    ElMessage.success("删除成功")
  }).catch(() => {})
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.teamId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 页面加载时
getList()
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.custom-input :deep(.el-input__inner) {
  font-size: 13px;
  height: 32px;
  line-height: 32px;
  padding: 0 12px;
}

.custom-input :deep(.el-textarea__inner) {
  font-size: 13px;
  padding: 8px 12px;
  min-height: 60px;
}

.custom-input :deep(.el-input__count) {
  font-size: 12px;
  background: transparent;
  height: 20px;
  line-height: 20px;
  padding: 0 4px;
}

.el-table {
  margin-top: 15px;
  font-size: 13px;
}

.el-table th {
  background-color: #f5f7fa !important;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-form-item__label {
  font-weight: 500;
}

.dialog-footer {
  text-align: right;
  padding-top: 10px;
}

.mb8 {
  margin-bottom: 8px;
}

.small-padding {
  padding-left: 5px;
  padding-right: 5px;
}

.fixed-width {
  min-width: 100px;
}
</style>
